package view.layer
{
	import control.manager.BitmapManager;
	import control.manager.LayerManager;
	import control.manager.MapManager;
	import control.manager.RoleManager;
	import control.util.GameUtil;
	
	import data.GameConst;
	import data.MapData;
	import data.NPCData;
	import data.PlayerData;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import view.Immortal;
	import view.item.ATT;
	import view.item.MouseClickEffect;
	import view.item.NormalAtt;
	import view.role.MapMonster;
	import view.role.MapNPC;
	import view.role.MapObject;
	import view.role.MapPlayer;
	import view.ui.UIMain;
	import view.ui.UIPopupLayer;
	import view.ui.UITaskTalk;
	import view.ui.UITaskTrack;

	public class GameLayer extends BaseLayer
	{
		
		public static const NAME  :String= "GAME_LAYER";
		/**
		 * 地图层 
		 */		
		public var mapLayer :MapLayer;
		/**
		 * 角色层 
		 */		
		public var roleLayer :RoleLayer;
		/**
		 * 玩家 
		 */		
		public var player :MapPlayer;
		/**
		 * 当前可视范围宽 
		 */		
		public var visibleWidth :int;
		/**
		 * 当前可视范围高 
		 */		
		public var visibleHeight :int;
		/**
		 * 攻击显示 
		 */		
		public var att :ATT;
		/**
		 * 当前地图的角色列表 
		 */		
		private var roleList :Array;
		
		/**
		 * 鼠标当前对象 
		 */		
		private var mouseOnObj :MapObject;
		/**
		 * 是否检查变换鼠标上的对象 
		 */		
		private var changeMouseOn :Boolean = true;
		/**
		 * 鼠标点击地图效果 
		 */		
		public var mouseClickEffects :MouseClickEffect;
		
		public function GameLayer(url :String = null,app :DisplayObject = null)
		{
			super();
			
			initGameView();
			this.roleList = RoleManager.instance.roleList;
		}
		/**
		 * 更新函数 
		 * 
		 */		
		public function update():void
		{
			//更新角色状态
			if(this.roleList && this.roleList.length > 0)
			{
				for each(var mapObj :MapObject in this.roleList)
				{
					if(mapObj.isAlive) //更新
					{
						mapObj.update();
					}else  //删除
					{
						RoleManager.instance.removeRole(mapObj);
					}
				}
			}
			
			//
			if(this.player.curClickObj is MapMonster && 
				this.player.curStatus == MapPlayer.START_ATT && 
				!this.att)
			{
				this.att = new NormalAtt(this.player,this.player.curClickObj as MapMonster);
				this.addChild(this.att);
			}
			
			if(this.att)
			{
				this.att.update();
			}

			
//			findMouseOnMapObj()
		}
		
		public function findMouseOnMapObj():void
		{
			var theMouseOn :MapObject;
			if(this.roleList.length > 0)
			{
				for(var index :int = this.roleList.length - 1; index >= 0; index--)
				{
					var mapObj :MapObject = this.roleList[index] as MapObject;
					if(mapObj.isAlive)
					{
						mapObj.update();
						
						if(mapObj.mouseCollision.checkMouseOn && 
							mapObj.parent)
						{
							if(theMouseOn)
							{
								var i1:int=0,i2:int=0;
								if(theMouseOn.parent == mapObj.parent)
								{
									i1 = theMouseOn.parent.getChildIndex(theMouseOn);
									i2 = theMouseOn.parent.getChildIndex(mapObj);
								}
								else if(theMouseOn.parent.parent && mapObj.parent.parent && theMouseOn.parent.parent == mapObj.parent.parent)
								{
									i1 = theMouseOn.parent.parent.getChildIndex(theMouseOn.parent);
									i2 = theMouseOn.parent.parent.getChildIndex(mapObj.parent);
								}
								if(i2 > i1)
								{
									theMouseOn = mapObj;
								}
							}else
							{
								theMouseOn = mapObj;
							}
						}
					}else
					{
						removeItemAt(index);
					}
				}
			}
			
			changleMouseOn(theMouseOn);
		}
		
		/**
		 * 改变鼠标当前的Onmouse对象 
		 * @param theMouseOn
		 * 
		 */		
		public function changleMouseOn(theMouseOn :MapObject):void
		{
			if(mouseOnObj == theMouseOn) return;   //同一个对象
			
			
			if(changeMouseOn)
			{
				if(mouseOnObj)
				{
					mouseOnObj.mouseOn = false;
					mouseOnObj = null;
				}
				
				if(theMouseOn)
				{
					mouseOnObj = theMouseOn;
					mouseOnObj.mouseOn = true;
				}
			}
		}
		
		public function removeItemAt(index :int):void
		{
			
		}
		
		public function mouseUpHandler(event :MouseEvent):void
		{
		}
		
		public function mouseDownHandler(event :MouseEvent):void
		{
		}
		
		/**
		 * 鼠标点击 
		 * @param event
		 * 
		 */		
		public function clickHandler(event :MouseEvent):void
		{
			if(event.target is RoleLayer) 
			{
				if(this.player)
				{
					var mapData :MapData = MapManager.instance.curMapData;
					var start :Point = new Point(this.player.x,this.player.y);
					var end :Point = new Point(this.mapLayer.mouseX,this.mapLayer.mouseY);
					this.player.findPath(start,end);
					//重置人物当前点击的对象
					this.player.curClickObj = null;
					//清空跨地图路径点
					RoleManager.instance.needWalkPoint = null;
					this.mouseClickEffects.showEffect();
					this.mouseClickEffects.x = end.x + 150;
					this.mouseClickEffects.y = end.y + 165;
					
					if(UIPopupLayer.I.isShow)
					{
						UIPopupLayer.I.hide();
					}
				}
			}else
			{
//				trace("寻路点击的不是角色层");
			}

			
			
		}
		/**
		 * 缩放大小 
		 */		
		public function resize():void
		{
			this.visibleWidth = stage.stageWidth;
			this.visibleHeight = stage.stageHeight;
			//更新地图层的可视范围宽高
			if(this.mapLayer)
			{
				if(this.visibleWidth > this.mapLayer.visbleW && this.visibleHeight > this.mapLayer.visbleH)
				{
					this.mapLayer.visbleW = this.visibleWidth;
					this.mapLayer.visbleH = this.visibleHeight;
				}
			}
			//锁定人物位置，保持中点显示
			if(this.player)
			{
				lockAt(this.player.x,this.player.y);
			}
			//检查是否需要加载地图块
			mapLayer.checkAndLoadMapTileItem();
			
			Immortal.instance.getVisibleWH();
		}
		
		override public function updatePosition(movePoint:Point=null, isResize:Boolean=false):void
		{
			//层块移动
			var p :Point;
			if(movePoint)
			{
				p = movePoint;
			}else
			{
				p  = new Point(this.x + this.sceneMoveSpeed.x, this.y + this.sceneMoveSpeed.y);
			}
			if(this.mapLayer.moveArea)
			{
				if(p.x > this.mapLayer.moveArea.right)
				{
					p.x = this.mapLayer.moveArea.right;
				}
				
				if(p.y > this.mapLayer.moveArea.bottom)
				{
					p.y = this.mapLayer.moveArea.bottom;
				}
			}
			
			this.x = p.x;
			this.y = p.y;
			//重置
			this.sceneMoveSpeed = new Point();
			
		}
		
		/**
		 * 初始化地图信息后开始配置场景 
		 * 
		 */		
		private function initGameView():void
		{
			//玩家信息
			var playerData :PlayerData = RoleManager.instance.playerData;
			var curMapID :int = playerData.curMapID;
			var curMapData :MapData = MapManager.instance.findByID(curMapID);
			if(!curMapData)
			{
				trace("fuck mapdata is null at GameLayer");
				return;
			}
			//设置可视范围
			this.visibleWidth = Immortal.instance.getVisibleWH().x;
			this.visibleHeight = Immortal.instance.getVisibleWH().y;
			
			//角色层
			this.roleLayer = new RoleLayer();
			LayerManager.instance.layerDict[RoleLayer.NAME] = roleLayer;
			
			//地图层
			mapLayer = new MapLayer();
			mapLayer.changeMap(curMapData,this.visibleWidth,this.visibleHeight);
			LayerManager.instance.layerDict[MapLayer.NAME] = mapLayer;
			
			this.addChild(mapLayer);
			this.addChild(this.roleLayer);
			
			
			//玩家
			this.player = new MapPlayer(playerData);
			var px :int = playerData.bornPoint.x;
			var py :int = playerData.bornPoint.y;
			var w :int = curMapData.mapWalkGapWidth;
			var h :int = curMapData.mapWalkGapHeight;
			var bornPoint :Point = MapManager.instance.getPixelPoint(w,h,px,py);
			this.player.x = bornPoint.x;
			this.player.y = bornPoint.y;
			this.player.gameLayer = this;
			RoleManager.instance.addRole(this.player);

			
			this.mouseClickEffects = new MouseClickEffect();
			if(this.mouseClickEffects)
			{
				this.addChild(this.mouseClickEffects);
				this.mouseClickEffects.hideEffect();
			}
			
			UITaskTrack.I.completeTask();
			
			
			
			//锁定人物镜头
			lockAt(this.player.x,this.player.y);
			
		}
		
		/**
		 * 锁定可视范围在某一点
		 * @param posX
		 * @param posY
		 * 
		 */		
		public function lockAt(posX :Number,posY :Number):void
		{
			//中心位置
			var layerPosX :Number = visibleWidth/2 - posX;
			var layerPosY :Number = visibleHeight/2 - posY;
			var mapData :MapData = MapManager.instance.curMapData;
			
			if(layerPosX > 0)
			{
				layerPosX = 0;
			}else if(layerPosX < visibleWidth - mapData.mapWidth)
			{
				layerPosX = visibleWidth - mapData.mapWidth;
			}
			
			
			if(layerPosY > 0)
			{
				layerPosY = 0;
			}else if(layerPosY < visibleHeight - mapData.mapHeight)
			{
				layerPosY = visibleHeight - mapData.mapHeight;
			}
			//更新位置显示
			this.updatePosition(new Point(layerPosX,layerPosY));
			
		}
	}
}